package com.laiat.yamba.activity;

import android.content.Context;
import android.database.Cursor;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.laiat.yamba.R;
import com.laiat.yamba.StatusData;

public class TimelineAdapter extends SimpleCursorAdapter {
	private static final String[] FROM = 
		{ StatusData.C_CREATED_AT, StatusData.C_USER, StatusData.C_TEXT };

	private static final int[] TO = { R.id.textCreatedAt, R.id.textUser, R.id.textText };

	private static ViewBinder VIEW_BINDER = new ViewBinder() {

		@Override
		public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
			if (view.getId() != R.id.textCreatedAt) {
				return false;
			}
			
			//Update the created at text to relative time
			long timestamp = cursor.getLong(columnIndex);
			((TextView)view).setText(DateUtils.getRelativeTimeSpanString(timestamp));
			
			return true;
		}
		
	};

	
	public TimelineAdapter(Context context, Cursor cursor) {
		super(context, R.layout.timeline_row, cursor, FROM, TO);
		setViewBinder(VIEW_BINDER);
	}
	
}
